//
//  ViewController.m
//  ClientDemo
//
//  Created by zc on 13-8-16.
//  Copyright (c) 2013年 zc. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)bgTaped:(id)sender {
    
}

- (IBAction)print:(id)sender {
    
    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    pic.delegate = self;
    //打印一个图像
    
    UIImage *image = [UIImage imageNamed:@"hh.png"];
    NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];

    if (pic && [UIPrintInteractionController canPrintData:data]) {
        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        printInfo.outputType = UIPrintInfoOutputGeneral;
        printInfo.jobName = @"hello";
        pic.printInfo = printInfo;
        pic.showsPageRange = YES;
        
        
        //打印一个网页
        UIWebView *webView = [[UIWebView alloc]init];
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
        
        pic.printFormatter = [webView viewPrintFormatter];
        
        
        
        //这里可以是许多类型，参照官方文档
        //single NSData, NSURL, UIImage, ALAsset
//        pic.printingItem =data;
    
        void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
        ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
            if (!completed && error) {
                NSLog(@"Printing could not complete because of error: %@", error);
            }
        };
        
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            [pic presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
        } else {
            [pic presentAnimated:YES completionHandler:completionHandler];
        }

        
    }
    
    
    
    
    //        //这是打印文本格式
    //
    //         UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc]
    //         initWithText:@"Test Printer"];
    //         textFormatter.startPage = 0;
    ////         textFormatter.contentInsets = UIEdgeInsetsMake(0, 100, 0, 72.0); // 1 inch margins
    ////         textFormatter.maximumContentWidth = 6 * 72.0;
    //         pic.printFormatter = textFormatter;

    
    
    
}
@end
